package main

import (
	"fmt"
	"github.com/golang/protobuf/proto"
	"io/ioutil"
	"log"
	pb "mygozero/user-rpc/github.com/protocolbuffers/protobuf/examples/go/tutorialpb"
)

func main() {
	p := pb.Person{
		Id:    1234,
		Name:  "John Doe",
		Email: "jdoe@example.com",
		Phones: []*pb.Person_PhoneNumber{
			{Number: "555-4321", Type: pb.PhoneType_PHONE_TYPE_HOME},
		},
	}
	peoples := make([]*pb.Person, 0)
	peoples = append(peoples, &p)
	book := &pb.AddressBook{People: peoples}

	// Write the new address book back to disk.
	out, err := proto.Marshal(book)
	if err != nil {
		log.Fatalln("Failed to encode address book:", err)
	}
	if err := ioutil.WriteFile("fname", out, 0644); err != nil {
		log.Fatalln("Failed to write address book:", err)
	}

	//Read the existing address book.
	in, err := ioutil.ReadFile("fname")
	if err != nil {
		log.Fatalln("Error reading file:", err)
	}
	readbook := &pb.AddressBook{}
	if err := proto.Unmarshal(in, readbook); err != nil {
		log.Fatalln("Failed to parse address book:", err)
	}
	fmt.Println(readbook.People)
}
